import java.util.List;
import java.util.LinkedList;

public class EBNFClass{
    private List<SintaxRule> sr;
    private String id;
    private List<Simbol> simbols;

    public EBNFClass (){
		this.sr=new LinkedList<SintaxRule>();
		this.id="";
    }
    
    public EBNFClass(String id){
		this.id=id;
		this.sr=new LinkedList<SintaxRule>();
		this.simbols=new LinkedList<Simbol>();
    }

    public EBNFClass(String id, List<SintaxRule> sr){
		this.id=id;
		this.sr=sr;
		this.simbols=new LinkedList<Simbol>();
    }
    
    public EBNFClass(String id, List<Simbol> simbols, List<SintaxRule> sr){
		this.id=id;
		this.sr=sr;
		this.simbols=simbols;
    }

    public List<Simbol> getSimbols(){
		return this.simbols;
    }

    public List<SintaxRule> getSintaxRule(){
		return this.sr;
    }

    public String getId(){
		return this.id;
    }

    public void setId(String id){
		this.id=id;
    }

    public void addSR(SintaxRule s){
		this.sr.add(s);
    }

    public void addSimbol(Simbol s){
		this.simbols.add(s);
    }

    public void setType(String type){
		for(int i=0;i<simbols.size();i++)
		    simbols.get(i).setType(type);
    }
    
    public void addSimbols(EBNFClass e2){
		for(int i=0;i<e2.getSimbols().size();i++)
		    this.addSimbol(e2.getSimbols().get(i));
    }

	public void setSR(List<SintaxRule> sr){
		this.sr=sr;
	}

	public String toString(){
		String termin="";
		String nontermin="";
		boolean haytermin=false;
		boolean haynontermin=false;

		String s="ebnf "+getId()+"{\n";
		for(int i=simbols.size()-1;i>=0;i--){
			if(simbols.get(i).getType()=="terminal"){
				if(haytermin==true)termin+=", ";
				termin+=simbols.get(i).getValue();				
				haytermin=true;
			}
			if(simbols.get(i).getType()=="nonterminal"){
				if(haynontermin==true)nontermin+=", ";
				nontermin+=simbols.get(i).getValue();
				haynontermin=true;
			}
		}

		if(haytermin==true)s+="terminal "+termin+";\n";
		if(haynontermin==true)s+="non terminal "+nontermin+";\n";			

		for(int i=sr.size()-1;i>=0;i--)
			s+=sr.get(i).toString()+"\n";

		s+="}\n";
		return s;
	}	

	
}
